package activiti2;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分配个人任务的三种方式
 */
public class FenPerRenWu {

    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();


    //流程图中配置任务节点变量

    @Test
    public void deployementAndStartProcess(){
        Deployment deployment = processEngine.getRepositoryService()
                .createDeployment()
                .addClasspathResource("diagrams/fenpeirenwu.bpmn")
                .name("个人任务演示")
                .deploy();
        System.out.println("部署Id：" + deployment.getId());

        Map<String,Object> variables = new HashMap<>();
        variables.put("userID","鲍康");
        ProcessInstance pi = processEngine.getRuntimeService()
                .startProcessInstanceByKey("myProcess_7",variables);
        System.out.println("流程实例ID："+pi.getId());
        System.out.println("流程定义的ID："+pi.getProcessDefinitionId());
    }

    /**查询我的个人任务*/
    @Test
    public void findPersonalTaskList(){
        String assignee = "鲍康";
        List<Task> list = processEngine.getTaskService()
                .createTaskQuery()
                .taskAssignee(assignee)
                .list();
        if(list != null && list.size() > 0){
            for(Task task:list){
                System.out.println("任务的办理人："+task.getAssignee());
                System.out.println("任务ID："+task.getId());
                System.out.println("任务名称："+task.getName());
                System.out.println("任务的创建时间："+task.getCreateTime());
                System.out.println("流程实例ID："+task.getProcessInstanceId());
                System.out.println("#######################################");
            }
        }
    }

    /**
     * 完成任务
     */
    @Test
    public void completeTask(){
        //任务ID
        String taskId = "75009";
        processEngine.getTaskService().complete(taskId);
        System.out.println("完成任务："+taskId);

    }

}
